var xmlHttp;
var ajaxAction;
function createXmlHttp(){
	if(window.XMLHttpRequest){
	       xmlHttp = new XMLHttpRequest();
	}else{
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}
function doSubmitSave(url,input){
	$("#fileupload").val($("#bester").val());
	if($('input[name=contentId]').val()==null){
	}else{
		$('input[name=content.contentId]').val($('input[name=contentId]').val());
	}
	var contentId = $('input[name=content.contentId]').val();
	var cateId = $('input[name=content.category.categoryId]').val();
	var thisForm=input.form;
	thisForm.action = ajaxAction+"/content/contentattach-save.action?url=" + encodeURI(url)+"&content.contentId="+contentId+"&content.category.categoryId="+cateId;
	thisForm.submit();
}
function doSubmitSaveFlash(url,input){
	var str = (url.substr(url.length -5)).substr((url.substr(url.length -5)).indexOf('.')+1).toLowerCase();
	if(str=="flv"){
		if($('input[name=flashContentId]').val()==null){
		}else{
			$('input[name=flashContent.contentId]').val($('input[name=flashContentId]').val());
		}
		var contentId = $('input[name=flashContent.contentId]').val();
		var cateId = $('input[name=flashContent.category.categoryId]').val();
		var thisForm=input.form;
		thisForm.action = ajaxAction+"/content/contentattach-saveflash.action?url=" + encodeURI(url)+"&flashContent.contentId="+contentId+"&flashContent.category.categoryId="+cateId;
		thisForm.submit();
	}else{
		alert("动画只支持flv格式上传!");
		return false;
	}
	
}
function doSubmitSaveVideo(url,input){
	var str = (url.substr(url.length -5)).substr((url.substr(url.length -5)).indexOf('.')+1).toLowerCase();
	if(str=="wmv"||str=="asx"){
		if($('input[name=videoContentId]').val()==null){
		}else{
			$('input[name=videoContent.contentId]').val($('input[name=videoContentId]').val());
		}
		var contentId = $('input[name=videoContent.contentId]').val();
		var cateId = $('input[name=videoContent.category.categoryId]').val();
		var thisForm=input.form;
		thisForm.action = ajaxAction+"/content/contentattach-savevideo.action?url=" + encodeURI(url)+"&videoContent.contentId="+contentId+"&videoContent.category.categoryId="+cateId;
		thisForm.submit();
	}else{
		alert("视频只支持wmv/asx格式上传!");
		return false;
	}
}
function doSubmitSaveSoftware(url,input){
	var str = (url.substr(url.length -5)).substr((url.substr(url.length -5)).indexOf('.')+1).toLowerCase();
	if(str=="rar"||str=="zip"){
		if($('input[name=softwareContentId]').val()==null){
		}else{
			$('input[name=softwareContent.contentId]').val($('input[name=softwareContentId]').val());
		}
		var contentId = $('input[name=softwareContent.contentId]').val();
		var cateId = $('input[name=softwareContent.category.categoryId]').val();
		var thisForm=input.form;
		thisForm.action = ajaxAction+"/content/contentattach-savesoftware.action?url=" + encodeURI(url)+"&softwareContent.contentId="+contentId+"&softwareContent.category.categoryId="+cateId;
		thisForm.submit();
	}else{
		alert("软件只支持rar/zip格式上传!");
		return false;
	}
}
function doSubmitSaveSoftwarePic(url,input){
	$("#softwarePic").val($("#besterPic").val());
	var str = (url.substr(url.length -5)).substr((url.substr(url.length -5)).indexOf('.')+1).toLowerCase();
	if(str=="jpg"||str=="png"||str=="gif"||str=="jpeg"){
		if($('input[name=softwareContentId]').val()==null){
		}else{
			$('input[name=softwareContent.contentId]').val($('input[name=softwareContentId]').val());
		}
		var contentId = $('input[name=softwareContent.contentId]').val();
		var cateId = $('input[name=softwareContent.category.categoryId]').val();
		var thisForm=input.form;
		thisForm.action = ajaxAction+"/content/contentattach-savesoftwarepic.action?url=" + encodeURI(url)+"&softwareContent.contentId="+contentId+"&softwareContent.category.categoryId="+cateId;
		thisForm.submit();
	}else{
		alert("软件图片只支持jpg/png/gif/jpeg/zip格式上传!");
		return false;
	}
}
function doSubmitSavePicture(url,input){
	var str = (url.substr(url.length -5)).substr((url.substr(url.length -5)).indexOf('.')+1).toLowerCase();
	if($("input[name=uploadType]").val()==1){
		if(str=="jpg"||str=="png"||str=="gif"||str=="jpeg"){
			if($('input[name=pictureContentId]').val()==null){
			}else{
				$('input[name=pictureContent.contentId]').val($('input[name=softwareContentId]').val());
			}
			var contentId = $('input[name=pictureContent.contentId]').val();
			var cateId = $('input[name=pictureContent.category.categoryId]').val();
			var thisForm=input.form;
			thisForm.action = ajaxAction+"/content/contentattach-savepicture.action?url=" + encodeURI(url)+"&pictureContent.contentId="+contentId+"&pictureContent.category.categoryId="+cateId;
			thisForm.submit();
		}else{
			alert("图片只支持jpg/png/gif/jpeg格式上传!");
			return false;
		}
	}else{
		if(str=="zip"){
			if($('input[name=pictureContentId]').val()==null){
			}else{
				$('input[name=pictureContent.contentId]').val($('input[name=softwareContentId]').val());
			}
			var contentId = $('input[name=pictureContent.contentId]').val();
			var cateId = $('input[name=pictureContent.category.categoryId]').val();
			var thisForm=input.form;
			thisForm.action = ajaxAction+"/content/contentattach-savepicture.action?url=" + encodeURI(url)+"&pictureContent.contentId="+contentId+"&pictureContent.category.categoryId="+cateId;
			thisForm.submit();
		}else{
			alert("图片压缩包只支持zip格式上传!");
			return false;
		}
	}
	
	
}
function addFileParent(id){
	i=0;
	sendRequestParent("control=addFile&contentAttach.content.contentId="+id);
}
function sendRequestParent(params) {
    createXmlHttp();
    xmlHttp.onreadystatechange = showUploadFileParent;
    xmlHttp.open("POST", "contentattach-ajaxRequest.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(params+"&timestamp=" + new Date().getTime());
}
function sendRequest(params) {
    createXmlHttp();
    xmlHttp.onreadystatechange = showUploadFile;
    xmlHttp.open("POST", "contentattach-ajaxRequest.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(params+"&timestamp=" + new Date().getTime());
}
function showUploadFileParent() {
    if (xmlHttp.readyState == 4) {
    	$('#uploads',window.parent.document).html('');
        var tt = xmlHttp.responseText;
        if(tt!=null){
        	$("#attach",window.parent.document).show();
     	    $('#uploads',window.parent.document).html(tt.replace(/(value=\").+\"/i,"$1\""));
     	   window.parent.initBar();
        }
       var contentId = $("input[name=contentId]",window.parent.document).val();
       $('#uploadForm',window.parent.document).attr("action",ajaxAction+"/content/contentattach-iframeRequest.action?contentAttach.content.contentId="+contentId);
        $('#uploadForm',window.parent.document).submit();
        window.parent.startStatusCheck(contentId);
        
   }
}
function showUploadFile() {
    if (xmlHttp.readyState == 4) {
        var tt = xmlHttp.responseText;
        if(tt!=null){
        	$("#attach").show();
        	$("#before").hide();
        	$("#uploads").show();
     	    $('#uploads').html(tt.replace(/(value=\").+\"/i,"$1\""));
     	    if($('input[name=softwareContent.softwarePic]').val()!=""){
     	    	$("#softPic").attr("src",$('input[name=softwareContent.softwarePic]').val());
     	    }
        }
   }
}
var i = 0;
function startStatusCheck(contentId){
    createXmlHttp();
    i=1;
    xmlHttp.onreadystatechange = showUploadFilePercent;
    xmlHttp.open("POST", "contentattach-returnValue.action", true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send("contentAttach.content.contentId=" + contentId);
}
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

function showUploadFilePercent()
{	
    if (xmlHttp.readyState == 4) {
           if(xmlHttp.responseText.trim()!=''){
        	   var x=xmlHttp.responseText.split("@")[1];
	           	if(x<99)
	           	{
	           		x++;
	           		percentBar(x);	
	           	}
	           	var contentId = $("input[name=contentId]").val();
	           	startStatusCheck(contentId);
           }else{
        	   var x=100;
	           percentBar(x);
	           if($("input[name=flag]").val()=='picture'){
	        	   setTimeout(function(){
		        	   listFile($("input[name=contentId]").val(),$("input[name=flag]").val());
					},6000);
	           }
	           $("#fileupload").val("");
           }
  }
}
function listFile() {
	sendRequest("control=listFile");
}
function addFile(){
	   i=0;
	   sendRequest("control=addFile");
	}
function emptyFile() {
 sendRequest("control=emptyFile&attachId=&");
}
function listFile(t) {
	if(t==''){
		sendRequest("control=listFile&flag=");
	}else{
		sendRequest("control=listFile&contentAttach.content.contentId="+t+"&flag=");
	}
}
function listFile(t,flag) {
	if(t==''){
		sendRequest("control=listFile&flag=");
	}else{
		sendRequest("control=listFile&contentAttach.content.contentId="+t+"&flag="+flag);
	}
}
function cancleFile() {
 createXmlHttp();
 xmlHttp.onreadystatechange = function(){
 if (xmlHttp.readyState == 4) {
     $('#uploads').val();
 	document.uploadForm.submit();
     document.getElementById("uploads").innerHTML = xmlHttp.responseText;
     var tt = $('#upload').html();
     if(tt!=null){
  	   $('#upload').html(tt.replace(/(value=\").+\"/i,"$1\""));
     }
     //document.getElementById("upload").outerHTML=document.getElementById("upload").outerHTML.replace(/(value=\").+\"/i,"$1\"");
     document.getElementById("cleanbutton").disabled="";
	    document.getElementById("bester").disabled="";
	    document.getElementById("canclebutton").disabled="true";
	    //alert('取消成功!');
     addFile($('input[name=contentId]').val());	    
 }    
 };
 xmlHttp.open("POST", "contentattach-ajaxRequest.action", true);
 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlHttp.send("control=cancleFile&timestamp=" + new Date().getTime());
}
function delFile(fileId,type) {
 createXmlHttp();
 xmlHttp.onreadystatechange = function(){
 if (xmlHttp.readyState == 4) {
	    listFile($('input[name=content.contentId]').val(),type);
 }    
 };
 xmlHttp.open("POST", "contentattach-ajaxRequest.action", true);
 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlHttp.send("control=delFile&contentAttach.attachId="+fileId+"&timestamp=" + new Date().getTime());
}
